const http = require('http')
const qs = require('querystring')
const config = require('../config')
const apiaddress = require('./apiaddress')

//发送邮件
const sendCode = async (ctx, next) => {
    let {
        sendTo
    } = ctx.request.body;
    let params = {
        sendTo: sendTo
    };
    let postdata = qs.stringify(params);
    const result = new Promise((resolve, reject) => {
        let opt = {
            hostname: config.mailhost,
            port: config.portEmaild,
            path: apiaddress.sendCode,
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Content-Length": postdata.length
            }
        };
        let req = http.request(opt, res => {
            res.setEncoding("utf8");
            let resdata = "";
            res.on("data", chunk => {
                resdata += chunk;
            });
            res.on("end", () => {
                resolve(JSON.parse(resdata));
            });
        });
        req.on("error", e => {
            reject(e);
        });
        req.write(postdata);
        req.end();
    });
    let data = await result;
    if (data.code) {
        ctx.error(data);
    } else {
        ctx.success(data);
    }
}
//重置密码
const sendCodetwo = async (ctx, next) => {
    let {
        email,
        password,
        checkCode
    } = ctx.request.body;
    // let {email,password} = ctx.request.body;
    let params = {
        email: email,
        password,
        password,
        checkCode,
        checkCode
    };
    // let params = {email, password};
    let postdata = qs.stringify(params);
    const result = new Promise((resolve, reject) => {
        let opt = {
            hostname: config.hostname,
            port: config.port,
            path: `/yesUser/forgetPassword`,
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Content-Length": postdata.length
            }
        };
        let req = http.request(opt, res => {
            res.setEncoding("utf8");
            let resdata = "";
            res.on("data", chunk => {
                resdata += chunk;
            });
            res.on("end", () => {
                resolve(JSON.parse(resdata));
            });
        });
        req.on("error", e => {
            reject(e);
        });
        req.write(postdata);
        req.end();
    });
    let data = await result;
    if (data.code) {
        ctx.error(data);
    } else {
        ctx.success(data);
    }
};
const getarticle = async () => {
    //获取上线楼层新闻
    const result = new Promise((resolve, reject) => {
        let opt = {
            hostname: config.hostname,
            port: config.port,
            path: `/yesTop/ArticleList?status=1&p=1&s=200`,
            method: "GET"
        };
        let req = http.request(opt, res => {
            res.setEncoding("utf8");
            let resdata = "";
            res.on("data", chunk => {
                resdata += chunk;
            });
            res.on("end", () => {
                let responesedata = JSON.parse(resdata);
                if (responesedata.code == 200) {
                    if (responesedata.data.list.length) {
                        let createdobj = Object.create(null),
                            resultarr = [];
                        responesedata.data.list.forEach((ele, num) => {
                            if (!createdobj[ele.code]) {
                                createdobj[ele.code] = {
                                    code: ele.code,
                                    flooryesaDescribe: ele.list[0].yesnDescribe,
                                    data: []
                                };
                            }
                            createdobj[ele.code].data.push(ele);
                        });
                        for (let key in createdobj) {
                            resultarr.push(createdobj[key]);
                        }
                        resolve(resultarr);
                    } else {
                        resolve([]);
                    }
                } else {
                    resolve([]);
                }
            });
        });
        req.on("error", e => {
            reject(e);
        });
        req.end();
    });
    return result;
};
const getNavigationList = async (ctx, next) => {
    //获取上线楼层新闻
    const result = new Promise((resolve, reject) => {
        let opt = {
            hostname: config.hostname,
            port: config.port,
            path: `/yesTop/ArticleList?status=1&p=1&s=200`,
            method: "GET"
        };
        let req = http.request(opt, res => {
            res.setEncoding("utf8");
            let resdata = "";
            res.on("data", chunk => {
                resdata += chunk;
            });
            res.on("end", () => {
                let responesedata = JSON.parse(resdata);
                if (responesedata.code == 200) {
                    if (responesedata.data.list.length) {
                        let createdobj = Object.create(null),
                            resultarr = [];
                        responesedata.data.list.forEach((ele, num) => {
                            if (!createdobj[ele.code]) {
                                createdobj[ele.code] = {
                                    code: ele.code,
                                    flooryesaDescribe: ele.list[0].yesnDescribe,
                                    data: []
                                };
                            }
                            createdobj[ele.code].data.push(ele);
                        });
                        for (let key in createdobj) {
                            resultarr.push(createdobj[key]);
                        }
                        resolve(resultarr);
                    } else {
                        resolve([]);
                    }
                } else {
                    resolve([]);
                }
            });
        });
        req.on("error", e => {
            reject(e);
        });
        req.end();
    });
    let data = await result;
    if (data.code && data.code != 200) {
        ctx.error(data);
    } else {
        let response = {
            code: 200,
            data: data
        };
        ctx.success(response);
    }
};
const deleteyesSelectionadd = async (ctx, next) => {
    //删除用户自选
    const userId = ctx.session.userId;
    if (userId) {
        let {
            navigationCode,
            articleId,
            delStatus
        } = ctx.request.body;
        let params = {
            userId: userId,
            navigationCode,
            navigationCode,
            articleId,
            articleId,
            delStatus,
            delStatus
        };
        let postdata = qs.stringify(params);
        const result = new Promise((resolve, reject) => {
            let opt = {
                hostname: config.hostname,
                port: config.port,
                path: `/yesSelection/delete?userId=${userId}&navigationCode=${navigationCode}&articleId=${articleId}&delStatus=${delStatus}`,
                method: "GET",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                    "Content-Length": postdata.length
                }
            };
            // path: `/yesTop/NavigationList?status=1&p=1&s=100`,
            let req = http.request(opt, res => {
                res.setEncoding("utf8");
                let resdata = "";
                res.on("data", chunk => {
                    resdata += chunk;
                });
                res.on("end", () => {
                    resolve(JSON.parse(resdata));
                });
            });
            req.on("error", e => {
                reject(e);
            });
            req.write(postdata);
            req.end();
        });
        let data = await result;
        if (data.code) {
            ctx.error(data);
        } else {
            ctx.success(data);
        }
    } else {
        let data = {
            code: 200,
            data: null
        };
        ctx.success(data);
    }
};
const yesSelectionadd = async (ctx, next) => {
    //添加用户自选
    const userId = ctx.session.userId;
    let {
        navigationCode,
        articleId
    } = ctx.request.body;
    // let {email,password} = ctx.request.body;
    let params = {
        userId: userId,
        navigationCode,
        navigationCode,
        articleId,
        articleId
    };
    // let params = {email, password};
    let postdata = qs.stringify(params);
    const result = new Promise((resolve, reject) => {
        let opt = {
            hostname: config.hostname,
            port: config.port,
            path: `/yesSelection/add`,
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Content-Length": postdata.length
            }
        };
        let req = http.request(opt, res => {
            res.setEncoding("utf8");
            let resdata = "";
            res.on("data", chunk => {
                resdata += chunk;
            });
            res.on("end", () => {
                resolve(JSON.parse(resdata));
            });
        });
        req.on("error", e => {
            reject(e);
        });
        req.write(postdata);
        req.end();
    });
    let data = await result;
    if (data.code) {
        ctx.error(data);
    } else {
        ctx.success(data);
    }
};
module.exports = {
    sendCode,
    sendCodetwo,
    getarticle,
    deleteyesSelectionadd,
    yesSelectionadd,
    getNavigationList
}